package com.haierCamera.customapplication.ui.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.CacheRepo;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.databinding.HzklActivityUserSettingBinding;
import com.haierCamera.customapplication.utils.DataCacheUtils;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserSettingActivity extends BaseActivity {
    private HzklActivityUserSettingBinding binding;
    @Inject
    CacheRepo repo;
    @Inject
    UserRepo userRepo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_user_setting);
        initData();
    }

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        binding.userLayout.setOnClickListener(view -> startActivity(new Intent(HZKLUserSettingActivity.this, HZKLUserAccountActivity.class)));
        binding.setInfoLayout.setOnClickListener(view -> startActivity(new Intent(HZKLUserSettingActivity.this, HZKLUserMsgSettingActivity.class)));
        binding.setFeedbackLayout.setOnClickListener(view -> startActivity(new Intent(HZKLUserSettingActivity.this, HZKLUserFeedBackActivity.class)));
        binding.setClearLayout.setOnClickListener(view -> removeCache());
        binding.setOutLayout.setOnClickListener(view -> new AlertDialog.Builder(HZKLUserSettingActivity.this).setMessage("退出登录")
                .setNegativeButton("取消", (dialogInterface, i) -> dialogInterface.dismiss()).setPositiveButton("确定", (dialogInterface, i) -> {
                    editOut();
                    dialogInterface.dismiss();
                }).show());
        getCache();
    }

    private void editOut() {
        userRepo.logout().observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    SharedPreferencesUtils.remove(Constants.SP_USER_TOKEN);
                    SharedPreferencesUtils.remove(Constants.SP_USER_PHONE);
                    SharedPreferencesUtils.remove(Constants.SP_USER_HEAD_URL);
                    //startActivity(new Intent(HZKLUserSettingActivity.this, HZKLLoginActivity.class));
                    finish();
                    //AppManager.getAppManager().finishAllActivity();
                    break;
            }
        });
    }

    private void getCache() {
        try {
            String cacheSize = DataCacheUtils.getTotalCacheSize(HZKLUserSettingActivity.this);
            binding.size.setText(cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
     /*   repo.getCacheSize().observe(this, stringResource -> {
            switch (stringResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    break;
                case SUCCESS:
                    binding.size.setText(stringResource.data);
                    break;
            }
        });*/
    }

    private void removeCache() {
        showLoadingDialog();
        DataCacheUtils.clearAllCache(HZKLUserSettingActivity.this);
        new Handler().postDelayed(() -> {
            getCache();
            dismissLoadingDialog();
        },1000);
        /*repo.removeCache().observe(this, stringResource -> {
            switch (stringResource.status) {
                case LOADING:
//                    showLoadingDialog();
                    break;
                case ERROR:
//                    dismissLoadingDialog();
                    Toast.makeText(this, stringResource.errorMessage, Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
//                    dismissLoadingDialog();
                    getCache();
                    break;
            }
        });*/
    }
}
